package com.myapp.ruyixing.ui;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ModifyPassword;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class ModifyPasswordActivity extends Activity implements OnClickListener {

	RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				ModifyPassword modifyPassword = (ModifyPassword) o;
				onLoadingDataFinished(true, modifyPassword);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_password);
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
		findViewById(R.id.btn_ok).setOnClickListener(this);

		String nickname = LoginHelper.getInstance().getNickName();
		if (!TextUtils.isEmpty(nickname)) {
			((TextView) findViewById(R.id.user_nickname)).setText(nickname);
		}
	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_ok) {
			String originPass = ((EditText) findViewById(R.id.edit_origin_pass))
					.getText().toString();
			if (TextUtils.isEmpty(originPass)) {
				showToast("请输入旧密码！");
				findViewById(R.id.edit_origin_pass).requestFocus();
				return;
			}
			String newPass = ((EditText) findViewById(R.id.edit_new_pass))
					.getText().toString();
			if (TextUtils.isEmpty(newPass)) {
				showToast("请输入新密码！");
				findViewById(R.id.edit_new_pass).requestFocus();
				return;
			}
			String confirmPass = ((EditText) findViewById(R.id.edit_confirm_pass))
					.getText().toString();
			if (TextUtils.isEmpty(confirmPass)) {
				showToast("请确认新密码！");
				findViewById(R.id.edit_confirm_pass).requestFocus();
				return;
			}
			if (!newPass.equals(confirmPass)) {
				showToast("两次输入的新密码不一致，请重新输入!");
				findViewById(R.id.edit_confirm_pass).requestFocus();
				return;
			}
			onLoadingDataStarted(LoginHelper.getInstance().getBDUSS(),
					originPass, newPass);
		}

	}

	private void enableView(boolean enable) {
		findViewById(R.id.edit_confirm_pass).setEnabled(enable);
		findViewById(R.id.edit_new_pass).setEnabled(enable);
		findViewById(R.id.edit_origin_pass).setEnabled(enable);
		findViewById(R.id.btn_ok).setEnabled(enable);
	}

	private void onLoadingDataStarted(String bduss, String originPass,
			String newPass) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);
		enableView(false);
		RYHttpClient.modifyPassword(this, mHttpResponseHandler, bduss,
				originPass, newPass);
	}

	private void onLoadingDataFinished(boolean succeed,
			ModifyPassword modifyPassword) {
		if (succeed && modifyPassword != null) {
			LoginHelper.getInstance().onModifyPassword(modifyPassword);
			showToast("密码修改成功！");
			finish();
		} else {
			((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
			showToast("密码修改失败！");
			enableView(true);
		}
	}

}
